<?php
namespace App\Http\Controllers\Tools;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Controllers\Weixin\WeixinServiceController as WeixinService;
use App\Models\V1\NewOrderTemplateModel;
use JWTAuth,JWTFactory;
use Tymon\JWTAuth\Exceptions\JWTException;
use Illuminate\Support\Facades\Redis;



class TestController extends Controller
{

    /**
     * 入口文档
     * */
    public function index(Request $request,$action)
    {

        if(method_exists($this,$action) && $action != 'index')
        {
            return $this->$action($request);
        }

        return "method $action is not exists";
    }


    /**
     * 发送微信信息
     * */
    public function send_wx_msg(Request $request)
    {

        $template   = new NewOrderTemplateModel();
        $weixinService  = new WeixinService();

        //给小秘书发送提示
        $orderInfo  = "学习官张维喜开发了一个新的会员";
        $template->openId   = "oPF8hxIQ6l_DBk_wHqbdjqDSmA30";
        $template->url      = "http://wx.laohoulundao.com/admin_system";
        $template->orderInfo= $orderInfo;
        $template->otherInfo= "订单基本信息";
        $template->orderName = "分会报单";
        $template->orderSn  = time();
        $template->orderTime = date_time();

        $weixinService->send_chapter_order_template($template);
    }


    public function test()
    {
        return 'It work !';
    }


    public function token_test(Request $request)
    {


        $customClaims = [
            //'foo' => 'bar',
            //'baz' => 'bob',
            'sub' => 'zhweixi@sina.cn'
        ];

        $payload    = JWTFactory::make($customClaims);
        $token      = JWTAuth::encode($payload);
        $token      = $token->get();
        $userId     = 10;
        $info   = ['token'=>$token,'user_id'=>$userId,'created_at'=>time()];
        $key    = 'token_'.md5($token);
        Redis::set($key,json_encode($info));
        $info = Redis::get($key);

        $info   = \GuzzleHttp\json_decode($info);

        dd($info);
        //$payload = JWTFactory::make($customClaims);
        //$token = JWTAuth::encode($payload);


        // grab credentials from the request
        $credentials = $request->only('email', 'password');
        return $credentials;
        try {
            // attempt to verify the credentials and create a token for the user
            if (! $token = JWTAuth::attempt($credentials)) {
                return response()->json(['error' => 'invalid_credentials'], 401);
            }
        } catch (JWTException $e) {
            // something went wrong whilst attempting to encode the token
            return response()->json(['error' => 'could_not_create_token'], 500);
        }

        // all good so return the token
        return response()->json(compact('token'));


    }


}